window.addEventListener( 'load', main, false );
var selectedTodo = null;

function main( )
{	
	if( typeof window.arguments[0].selectedTodoID != 'undefined' )
	{
		Log.debug( typeof window.arguments[0].selectedTodoID );
		Log.debug( 'sel id: '+ window.arguments[0].selectedTodoID );
		selectedTodo = tdt_getTodoById( window.arguments[0].selectedTodoID );
		$('tdt-todo-title').value = selectedTodo.title;
		$('tdt-todo-description').value = selectedTodo.description;
		
		Log.debug( selectedTodo );
	}
}

function tdt_newTodo( )
{
	var currentTodoListID = window.arguments[0].listID;
	
	var title = $('tdt-todo-title').value;
	var description = $('tdt-todo-description').value;

	if( title == '' )
	{
		alert( 'Don`t leave empty the title of the new todo, please.' );
		return false;
	}

	// oh, passed the checks
	//save the todo then
	if( selectedTodo !== null )
	{
		selectedTodo.title = title;
		selectedTodo.description = description;
		tdt_editTodo( selectedTodo );
		Log.debug( 'The todo was edited.' );
		return true;
	}
	else if( selectedTodo === null && Todo( null, currentTodoListID, title, description ).saveToFile( ) )
	{
		Log.debug( 'A new todo was born. ' );
	}
	else
	{
		Log.debug( 'The todo was stuck somwhere.' )
	}
	return true;
}